package com.mornd.storage.controller;

import com.mornd.storage.service.FileStorageService;
import com.mornd.storage.utils.FileUtil;
import com.mornd.storage.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: mornd
 * @dateTime: 2023/8/2 - 0:07
 */

@RefreshScope
@Controller
@RequestMapping("/fileStorage")
public class UploadController {
    @Resource
    private FileStorageService fileStorageService;

    @Value("${upload.dirPath:}")
    private String dirPath;

    /**
     * 单文件上传
     *
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) {
        // todo 文件格式校验
        Map<String, Object> result = new HashMap<>(2);
        if (file == null || file.isEmpty()) {
            result.put("success", false);
            result.put("msg", "文件不能为空");
            return result;
        }

        try {
            // 生成随机名称
            String fileName = SnowFlake.nextSnowflakeId()
                    + FileUtil.getSuffix(file.getOriginalFilename());

            String url = fileStorageService.upload(dirPath, fileName, file);
            result.put("success", true);
            result.put("urlPath", url);

        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("msg", "文件上传失败");
        }
        return result;
    }

    /**
     * 多文件上传
     *
     * @param fileList
     * @return
     */
    @ResponseBody
    @PostMapping("/multUpload")
    public Object multUpload(@RequestParam("file") MultipartFile[] fileList) {
        // todo 文件格式校验
        Map<String, Object> result = new HashMap<>(2);
        if (fileList == null || fileList.length == 0) {
            result.put("success", false);
            result.put("msg", "文件不能为空");
            return result;
        }

        if (fileList.length > 20) {
            result.put("success", false);
            result.put("msg", "文件个数超出最大限制");
            return result;
        }

        // 收集地址url
        String[] urlPaths = new String[fileList.length];

        for (int i = 0; i < fileList.length; i++) {
            MultipartFile file = fileList[i];
            if (file == null || file.isEmpty()) {
                urlPaths[i] = "";
                continue;
            }
            try {
                // 生成随机名称
                String fileName = SnowFlake.nextSnowflakeId()
                        + FileUtil.getSuffix(file.getOriginalFilename());

                String url = fileStorageService.upload(null, fileName, file);

                urlPaths[i] = url;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        result.put("success", true);
        result.put("urlPaths", urlPaths);

        return result;
    }

    @ResponseBody
    @PostMapping("/delete")
    public Object delete(@RequestBody Map<String, String> params) {
        String urlPath = params.get("urlPath");
        Map<String, Object> result = new HashMap<>(2);
        result.put("success", true);
        if (!StringUtils.hasText(urlPath)) {
            return result;
        }

        try {
            fileStorageService.delete(urlPath);
            return result;
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "删除失败");
        }
        return result;
    }
}
